package participation.Impl;

import cn.hutool.core.util.StrUtil;
import participation.dao.YzxJudgeDao;
import participation.domain.YzxJudge;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class YzxJudgeDaoImpl implements YzxJudgeDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(YzxJudge yzxJudge) {
        String sql = "insert into yzx_judge\n" +
                "        (id,studentId,isprincipal,teacherId,projectId) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,yzxJudge.getId());
            psmt.setLong(2,yzxJudge.getStudentId());
            psmt.setInt(3,yzxJudge.getIsprincipal());
            psmt.setLong(4,yzxJudge.getTeacherId());
            psmt.setLong(5,yzxJudge.getProjectId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long judge_id) {
        String sql = "delete from yzx_judge where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,judge_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(YzxJudge yzxJudge) {
        String sql = "update yzx_judge set";
        List<Object> param = new ArrayList<>();
        if (yzxJudge == null) {
            return;
        } else {
            if (yzxJudge.getId() != null) {
                sql += " id = ?,";
                param.add(yzxJudge.getId());
            }
            if(yzxJudge.getStudentId() != null) {
                sql += "studentId = ?,";
                param.add(yzxJudge.getStudentId());
            }
            if(yzxJudge.getIsprincipal() != null) {
                sql += "isprincipal = ?,";
                param.add(yzxJudge.getIsprincipal());
            }
            if(yzxJudge.getTeacherId() != null) {
                sql += "teacherId = ?,";
                param.add(yzxJudge.getTeacherId());
            }
            if(yzxJudge.getProjectId() != null) {
                sql += "projectId = ?,";
                param.add(yzxJudge.getProjectId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,yzxJudge.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public YzxJudge selectById(Long id) {
        String sql = "select * from yzx_judge where id = ?";
        YzxJudge yzxJudge = new YzxJudge();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                yzxJudge.setId(rs.getLong(1));
                yzxJudge.setStudentId(rs.getLong(2));
                yzxJudge.setIsprincipal(rs.getInt(3));
                yzxJudge.setTeacherId(rs.getLong(4));
                yzxJudge.setProjectId(rs.getLong(5));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return yzxJudge;
    }

    @Override
    public List<YzxJudge> selectList(YzxJudge yzxJudge) {
        String sql = "select * from yzx_judge where 1=1";
        List<YzxJudge> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (yzxJudge == null) {
            sql = "select * from yzx_judge";
        } else {
            if (yzxJudge.getId() != null) {
                sql += " and id = ?";
                param.add(yzxJudge.getId());
            }
            if(yzxJudge.getStudentId() != null) {
                sql += " and studentId = ?";
                param.add(yzxJudge.getStudentId());
            }
            if(yzxJudge.getIsprincipal() != null) {
                sql += " and isprincipal = ?";
                param.add(yzxJudge.getIsprincipal());
            }
            if(yzxJudge.getTeacherId() != null) {
                sql += " and teacherId = ?";
                param.add(yzxJudge.getTeacherId());
            }
            if(yzxJudge.getProjectId() != null) {
                sql += " and projectId = ?";
                param.add(yzxJudge.getProjectId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                YzxJudge yzxJudge1 = new YzxJudge();
                yzxJudge1.setId(rs.getLong(1));
                yzxJudge1.setStudentId(rs.getLong(2));
                yzxJudge1.setIsprincipal(rs.getInt(3));
                yzxJudge1.setTeacherId(rs.getLong(4));
                yzxJudge1.setProjectId(rs.getLong(5));
                list.add(yzxJudge1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
